package com.team.bus.module.user.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.zjframework.traceframe.common.dao.util.ContextHolderUtils;
import org.zjframework.traceframe.common.service.impl.CommonServiceImpl;
import org.zjframework.traceframe.common.util.Encrypt;

import com.team.bus.module.common.pagemodel.SessionInfo;
import com.team.bus.module.user.dao.IUserDao;
import com.team.bus.module.user.model.User;
import com.team.bus.module.user.service.IUserService;

@Service("userService")
public class UserServiceImpl extends CommonServiceImpl implements IUserService {

	private IUserDao userDao;

	public IUserDao getUserDao() {
		return userDao;
	}

	@Resource
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public List<String> gainMenus(String hql, Map<String, Object> param) {
		return userDao.findHql(hql, param);
	}

	@Override
	public boolean editCurrentUserPwd(SessionInfo sessionInfo, String oldPwd,
			String pwd) {  
		User u = userDao.get(User.class, sessionInfo.getId());
		if (u.getPassword().equalsIgnoreCase(Encrypt.e(oldPwd))) {// 说明原密码输入正确
			u.setPassword(Encrypt.e(pwd));
			userDao.updateBySqlString("update tuser set password='"
					+ Encrypt.e(pwd) + "' where id='" + sessionInfo.getId()
					+ "'");
			return true;
		}
		return false;
	}

	@Override
	public <T> T updateRows(T entity) {
		return userDao.updateRows(entity);
	}

	@Override
	public User gainSessionUser() {
		User user = (User) ContextHolderUtils.getSession().getAttribute("user");
		return user;
	}
	
	

}
